def main():
    students = {}
    print("=" * 30)
    print("     学生管理系统    ")
    print("1. 添加学生信息")
    print("2. 删除学生信息")
    print("3. 修改学生信息")
    print("4. 查询所有学生信息")
    print("0. 退出系统")
    print("=" * 30)
    while True:
        try:
            choice = int(input("请输入操作: "))
            if choice == 1:
                student_id = input("请输入学号: ")
                if student_id in students:
                    print("该学号已存在！")
                    continue
                name = input("请输入姓名: ")
                age = input("请输入年龄: ")
                gender = input("请输入性别: ")
                major = input("请输入专业: ")
                students[student_id] = {
                    "姓名": name,
                    "年龄": age,
                    "性别": gender,
                    "专业": major
                }
                print("添加成功！")
            elif choice == 2:
                if not students:
                    print("当前没有该学生信息")
                    continue
                student_id = input("请输入要删除的学号: ")
                if student_id in students:
                    del students[student_id]
                    print("删除成功")
                else:
                    print("学号不存在")
            elif choice == 3:
                if not students:
                    print("当前没有该学生信息")
                    continue
                student_id = input("请输入要修改的学号: ")
                if student_id not in students:
                    print("该学号不存在")
                    continue
                print("1. 修改姓名")
                print("2. 修改年龄")
                print("3. 修改性别")
                print("4. 修改专业")
                modify_choice = int(input("请选择要修改的信息编号: "))
                if modify_choice == 1:
                    new_name = input("请输入新姓名: ")
                    students[student_id]["姓名"] = new_name
                elif modify_choice == 2:
                    new_age = input("请输入新年龄: ")
                    students[student_id]["年龄"] = new_age
                elif modify_choice == 3:
                    new_gender = input("请输入新性别: ")
                    students[student_id]["性别"] = new_gender
                elif modify_choice == 4:
                    new_major = input("请输入新专业: ")
                    students[student_id]["专业"] = new_major
                else:
                    print("该选择无效")
                    continue
                print("修改成功")
            elif choice == 4:
                if not students:
                    print("当前没有学生信息")
                    continue
                print("\n" + "=" * 40)
                print(f"{'学号':<10}{'姓名':<10}{'年龄':<5}{'性别':<5}{'专业'}")
                print("-" * 40)
                for sid, info in students.items():
                    print(f"{sid:<10}{info['姓名']:<10}{info['年龄']:<5}{info['性别']:<5}{info['专业']}")
                print("=" * 40 + "\n")
            elif choice == 0:
                print("谢谢使用")
                break
            else:
                print("请输入有效的操作编号（0-4）")
        except ValueError:
            print("输入错误，请输入数字")
        except Exception as e:
            print(f"操作出错: {e}")
if __name__ == "__main__":
    main()